<?php
namespace Home\Model;
use Think\Model;
class OrderModel extends Model{

   	//获取用户的累计下单数量
    public function getTotalNums($map=''){

        $data = $this->where($map)->sum('order_num');
        return $data;
    }


   //从下到上获取团队的累计下单数量 累计三个月
    public function getTeamTotalNumsUpCum(&$userid,&$startTime='',&$endTime=''){
      $userinfo = M('user')->find($userid);
        $up_id=$userinfo['invite_id'];
        $up_userinfo = M('user')->find($up_id);
        $up_status=$up_userinfo['status'];
        $jb=M('jbset')->find();
         D('user')->set_xj_userstatus($userid);//执行下更新状态
         $self_totalnum=$this->get_group_num_bytime($userid,$startTime,$endTime);
                switch ($userinfo['status']) {

                    case '1':
                       $monthly=$jb['sj_jb2_orders'];
                       $total_con=$monthly*$jb['sj_jb2_mons'];
                       $save_sj['sj_ordernum']=1;
                       $save_sj_time['sj_time']=time();
                       $sj_invite='sj_invite';
                        break;
                    case '2':
                         $monthly=$jb['sj_jb3_orders'];
                         $total_con=$monthly*$jb['sj_jb3_mons'];
                          $save_sj['sj3_ordernum']=1;
                           $save_sj_time['sj3_time']=time();
                           $sj_invite='sj3_invite';
                        break;
                    case '3':
                          $monthly=$jb['sj_jb4_orders'];;
                          $total_con=$monthly*$jb['sj_jb4_mons'];
                           $save_sj['sj4_ordernum']=1;
                            $save_sj_time['sj4_time']=time();
                            $sj_invite='sj4_invite';
                        break;
                    
                    default:
                        # code...
                 

            }
       

             if ($self_totalnum>=$total_con ) {
              
                M('user')->where("id=$userid")->save($save_sj);  
               $sj_invite= M('user')->where("id=$userid")->getField($sj_invite);
               if ($sj_invite) {

                  $up_status=$userinfo['status']>=4?$userinfo['status']:$userinfo['status']+1;
               
                  M('user')->where("id=$userid")->setField("status",$up_status); 
                  M('user')->where("id=$userid")->save($save_sj_time); 
                  M('auth_group_access_home')->where("uid=$userid")->setField("group_id",$up_status);
                  
            
                          $up_up_id=$userinfo['invite_id'];
                          $up_up_status=M('user')->where('id='.$up_up_id)->getField('status');
                        if ($up_status>=$up_up_status  ) {

                         $beyond_months= M('jifen')->getField('beyond_months');
                          $beyond['beyond_end_time']= strtotime("+".$beyond_months."months");
                          $beyond['beyond_sj_id']=$up_up_id;
                          $beyond['f_id']=0;
                      M('user')->where("id=$userid")->save($beyond);
                      

                  }
                    

               }

             
            }
       
         //如果有上级判断上级是否升级
        if ($up_id) { 
            $this->getTeamTotalNumsUpCum($up_id,$startTime,$endTime);
        }

}


    //从下到上获取团队的累计下单数量 累计三个月  登录的时候判断
    public function getTeamTotalNumsUpCum_info(&$userid,&$startTime='',&$endTime=''){
      $userinfo = M('user')->find($userid);
        $up_id=$userinfo['invite_id'];
        $up_userinfo = M('user')->find($up_id);
        $up_status=$up_userinfo['status'];
        $jb=M('jbset')->find();

         $self_totalnum=$this->get_group_num_bytime($userid,$startTime,$endTime);
                switch ($userinfo['status']) {

                    case '1':
                       $monthly=$jb['sj_jb2_orders'];
                       $total_con=$monthly*$jb['sj_jb2_mons'];
                       $save_sj['sj_ordernum']=1;
                       $save_sj_time['sj_time']=time();
                       $sj_invite='sj_invite';
                        break;
                    case '2':
                         $monthly=$jb['sj_jb3_orders'];
                         $total_con=$monthly*$jb['sj_jb3_mons'];
                          $save_sj['sj3_ordernum']=1;
                           $save_sj_time['sj3_time']=time();
                           $sj_invite='sj3_invite';
                        break;
                    case '3':
                          $monthly=$jb['sj_jb4_orders'];;
                          $total_con=$monthly*$jb['sj_jb4_mons'];
                           $save_sj['sj4_ordernum']=1;
                            $save_sj_time['sj4_time']=time();
                            $sj_invite='sj4_invite';
                        break;
                    
                    default:
                        # code...
                 

            }
       

             if ($self_totalnum>=$total_con ) {
              
                M('user')->where("id=$userid")->save($save_sj);  
               $sj_invite= M('user')->where("id=$userid")->getField($sj_invite);
               if ($sj_invite) {

                  $up_status=$userinfo['status']>=4?$userinfo['status']:$userinfo['status']+1;
               
                  M('user')->where("id=$userid")->setField("status",$up_status); 
                  M('user')->where("id=$userid")->save($save_sj_time); 
                  M('auth_group_access_home')->where("uid=$userid")->setField("group_id",$up_status);
                  
                   if ($userinfo['invite_id']>0) {
                          $up_up_id=$userinfo['invite_id'];
                          $up_up_status=M('user')->where('id='.$up_up_id)->getField('status');
                        if ($userinfo['status']>=$up_up_status  ) {

                         $beyond_months= M('jifen')->getField('beyond_months');
                          $beyond['beyond_end_time']= strtotime("+".$beyond_months."months");
                          $beyond['beyond_sj_id']=$up_up_id;
                      M('user')->where("id=$userid")->save($beyond);

                  }
                    }

               }

             
            }

}




    //获取团队的累计下单数量截止目前
    public function getTeamTotalNumsNow($userid){
      
       $data=M('order_team')->where('team_id='.$userid)->sum('totalnum');
       return $data;
    }


      //获取团队的累计下单数量根据时间
    public function getTeamTotalNums($userid,$startTime='',$endTime=''){
       $map['team_id']=$userid;
      $map['addtime'] = array(array('egt',$startTime),array('elt',$endTime),'AND');
      $data=M('order_team')->where($map)->count();
        return $data;
    }


  

    //获取多个用户的总订单数
     public function getOrderNums_users($ids,$map=''){
            $con['user_id'] = array ('in',$ids);
           $con['paystatus'] = 1;
           $data = $this->where($con)->count();
        
        return $data;
    }

      public function getOrderNums_fl(&$userid,&$orderxf_num){
         $userinfo = M('user')->find($userid);
           $invite_id=$userinfo['invite_id']; 
           if ($invite_id>0) {
           $data['team_id']=$invite_id;
          $data['totalnum']=$orderxf_num;
          $data['addtime']=time();
          M('order_team')->add($data);
          M('user')->where('id='.$invite_id)->setInc('team_ordernums',$orderxf_num);
           $this->getOrderNums_fl($invite_id,$orderxf_num);
        }

    }



     //递归获取上级用户的总订单数  判断工资订单
     public function getOrderNums_up(&$userid,&$orderxf_num){

        $userinfo = M('user')->find($userid);
        $up_id=$userinfo['f_id'];
        
         $self_status=M('user')->where("id=$userid")->getField('status');
        $up_status=M('user')->where("id=$up_id")->getField('status');



        if ($up_id) {
           $data['fh_id']=$up_id;
          $data['totalnum']=$orderxf_num;
          $data['addtime']=time();
          M('order_team_fh')->add($data);
          M('user')->where('id='.$up_id)->setInc('fh_ordernums',$orderxf_num);
         
          $con_team['team_gz_status']=0;
          $con_team['team_id']=$up_id;
          $team_ordernum=M('order_team')->where($con_team)->sum('totalnum');

               if ($up_status>1) {
                //如果上级在业务员或者以上，则计算自己所在团队的业绩
                switch ($up_status) {
                    case '2':
                         $each=10;
                        break;
                    case '3':
                        $each=20;
                        break;
                    case '4':
                        $each=40;
                        break;    
                    default:
                        # code...
                        break;
                }
                if ($self_status==1 && $up_status>2) {
          
             $xf_total_nums=$this->xf_total_nums($up_id);

          $team_ordernum=$team_ordernum-$xf_total_nums; 

        }
               if ($team_ordernum>=$each) {
                    //应返还
                     $yfh_gz=floor($team_ordernum/$each);
            $gz['user_id']=$up_id;
            $gz['desc']='团队业绩达标';
            $fh_gz=M('order_gz')->where($gz)->count();
            $n=$yfh_gz-$fh_gz;

            for ($i=1; $i <=$n; $i++) { 
                unset($data);
                  $moshi=M('moshi')->where('status=1')->find();
                  $jifen=M('jifen')->find();
                $data['user_id']=$up_id;
                $data['addtime']=time();
                $data['desc']='团队业绩达标';
                $data['fh_weeks']=$moshi['moshi_gz_weeks'];
                $data['fh_cfjf']=$jifen['jifen_cf_gz'];

                    $a = date("w",$data['addtime']);
if($a =="0"){
      $d=date("Y-m-d 00:00:00",$data['addtime']);
}else{
    $d=date("Y-m-d 00:00:00",strtotime('next sunday',$data['addtime']));
}
     $data['begin_hf_time'] =strtotime($d)+604800; 
           
                $addgz=M('order_gz')->add($data);
                  $gz=M('order_gz')->find($addgz);
             
                 for ($i=1; $i <= $gz['fh_weeks']; $i++) { 
                $data_fh2['gz_id'] =$addgz;
                $data_fh2['hf_endtime'] =strtotime($d)+604800*$i;
                $data_fh2['cf_jifen'] =$gz['fh_cfjf'];
                if ($data_fh2['hf_endtime']<=$curtime) {
                  $data_fh2['status']=1;
                 M('user')->where("id=$userid")->setInc("cf_jifen",$gz['fh_cfjf']);
             $record['user_id']=$userid;
             $record['items']='工资订单每周返还财富积分';
             $record['jifen_num']='+'.$gz['fh_cfjf'];
             $record['addtime']=$data_fh2['hf_endtime'];
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);
                M('order_gz')->where("id=".$orderid)->setInc('already_fh_weeks');
                }else{
                  $data_fh2['status']=0;
                }
               
                M('order_gz_hf')->add($data_fh2);
            }

            }

                    
                
                
                    }
               


               

            }
              $this->getOrderNums_up($up_id,$orderxf_num);

        }
        
    }


      //递归获取上级用户的总订单数  判断累计订单是否升级
     public function getOrderNums_up_sj(&$userid){

        $userinfo = M('user')->find($userid);
        $up_id=$userinfo['invite_id'];
        $up_status=M('user')->where("id=$up_id")->getField('status');
        if ($up_id) {
          $con_team['team_id']=$up_id;
          $team_ordernum=M('order_team')->where($con_team)->sum('totalnum');
                //如果上级在业务员或者以上，则计算自己所在团队的业绩
                switch ($up_status) {
                    case '1':
                         $each=60*3;
                        break;
                    case '2':
                        $each=360*3;
                        break;
                    case '3':
                        $each=2160*3;
                        break;    
                    default:
                        # code...
                        break;
                }



                if ($self_status==1 && $up_status>2) {
          
             $xf_total_nums=$this->xf_total_nums($up_id);


          $team_ordernum=$team_ordernum-$xf_total_nums; 


        }

              
                     if ($team_ordernum>=$each) {
                       M('order_team')->where("team_id=$up_id")->setField("team_gz_status",1);
                  unset($data);
                  $moshi=M('moshi')->where('status=1')->find();
                  $jifen=M('jifen')->find();
                $data['user_id']=$up_id;
                $data['addtime']=time();
                $data['desc']='团队业绩达标';
                $data['fh_weeks']=$moshi['moshi_gz_weeks'];
                $data['fh_cfjf']=$jifen['jifen_cf_gz'];

                    $a = date("w",$data['addtime']);
if($a =="0"){
      $d=date("Y-m-d 00:00:00",$data['addtime']);
}else{
    $d=date("Y-m-d 00:00:00",strtotime('next sunday',$data['addtime']));
}
     $data['begin_hf_time'] =strtotime($d)+604800; 
           
                $addgz=M('order_gz')->add($data);
                  $gz=M('order_gz')->find($addgz);
             
                 for ($i=1; $i <= $gz['fh_weeks']; $i++) { 
                $data_fh2['gz_id'] =$addgz;
                $data_fh2['hf_endtime'] =strtotime($d)+604800*$i;
                $data_fh2['cf_jifen'] =$gz['fh_cfjf'];
                if ($data_fh2['hf_endtime']<=$curtime) {
                  $data_fh2['status']=1;
                 M('user')->where("id=$userid")->setInc("cf_jifen",$gz['fh_cfjf']);
             $record['user_id']=$userid;
             $record['items']='工资订单每周返还财富积分';
             $record['jifen_num']='+'.$gz['fh_cfjf'];
             $record['addtime']=$data_fh2['hf_endtime'];
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);
                M('order_gz')->where("id=".$orderid)->setInc('already_fh_weeks');
                }else{
                  $data_fh2['status']=0;
                }
               
                M('order_gz_hf')->add($data_fh2);
            }
                
                    }
               

            
              $this->getOrderNums_up_sj($up_id);

        }
        return false;
        
    }


      public function xf_total_nums($userid)
    { 
     
      $map['f_id']=$userid;
      $map['status']=1;
      $info=M('user')->where($map)->field('id')->select();
      foreach ($info as $key => $value) {
        $ids[]=$value['id'];
      }
      $map2['user_id']  = array('in',$ids);
      $map2['paystatus']  = 1;
      $map2['xf_gz_status']  = 0;
    $r=  M('order')->where($map2)->sum('order_num');
    if ($r>=10) {
             $yfh_gz=floor($r/10);
            $gz['user_id']=$up_id;
            $gz['desc']='自身直接推荐的'.$j['jb1'].'满10单';
            $fh_gz=M('order_gz')->where($gz)->count();
            $n=$yfh_gz-$fh_gz;

            for ($i=1; $i <=$n; $i++) { 
                unset($data);
                  $moshi=M('moshi')->where('status=1')->find();
                  $jifen=M('jifen')->find();
                $data['user_id']=$up_id;
                $data['addtime']=time();
                $data['desc']='自身直接推荐的'.$j['jb1'].'满10单';
                $data['fh_weeks']=$moshi['moshi_gz_weeks'];
                $data['fh_cfjf']=$jifen['jifen_cf_gz'];

                    $a = date("w",$data['addtime']);
if($a =="0"){
      $d=date("Y-m-d 00:00:00",$data['addtime']);
}else{
    $d=date("Y-m-d 00:00:00",strtotime('next sunday',$data['addtime']));
}
     $data['begin_hf_time'] =strtotime($d)+604800; 
           
                $addgz=M('order_gz')->add($data);
                  $gz=M('order_gz')->find($addgz);
             
                 for ($i=1; $i <= $gz['fh_weeks']; $i++) { 
                $data_fh2['gz_id'] =$addgz;
                $data_fh2['hf_endtime'] =strtotime($d)+604800*$i;
                $data_fh2['cf_jifen'] =$gz['fh_cfjf'];
                if ($data_fh2['hf_endtime']<=$curtime) {
                  $data_fh2['status']=1;
                 M('user')->where("id=$userid")->setInc("cf_jifen",$gz['fh_cfjf']);
             $record['user_id']=$userid;
             $record['items']='工资订单每周返还财富积分';
             $record['jifen_num']='+'.$gz['fh_cfjf'];
             $record['addtime']=$data_fh2['hf_endtime'];
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);
                M('order_gz')->where("id=".$orderid)->setInc('already_fh_weeks');
                }else{
                  $data_fh2['status']=0;
                }
               
                M('order_gz_hf')->add($data_fh2);
            }

            }


    }

      return $r;
    }



    //升级条件获取订单数量 自身加团队
    public function get_group_num($id,$months)
    {
      $end=time();

         $begin=mktime(0,0,0,date('m')-$months,1,date('Y'));
         
       $team_map['team_id']=$id;
         $team_map['addtime'] = array(array('egt',$begin),array('elt',$end),'AND');

              $order_map['user_id']=$id;
         $order_map['addtime'] = array(array('egt',$begin),array('elt',$end),'AND');
         //pjb/sj_convar_dump($search);exit();
       $cur_count= M('user')->where($search)->count();
       $num1 = M('order_team')->where($team_map)->sum('totalnum');
       $num2 = M('order')->where($order_map)->sum('order_num');
       $cur_num=$num1+$num2;
       return $cur_num;
    }




     //升级条件获取订单数量 自身加团队 根据时间端
    public function get_group_num_bytime($id,$begin='',$end='')
    {
  
         
       $team_map['team_id']=$id;
         $team_map['addtime'] = array(array('egt',$begin),array('elt',$end),'AND');

              $order_map['user_id']=$id;
              $order_map['paystatus']=1;
         $order_map['addtime'] = array(array('egt',$begin),array('elt',$end),'AND');
         //pjb/sj_convar_dump($search);exit();
       $cur_count= M('user')->where($search)->count();
       $num1 = M('order_team')->where($team_map)->sum('totalnum');
       $num2 = M('order')->where($order_map)->sum('order_num');
       $cur_num=$num1+$num2;
       return $cur_num;
    }


}
